package com.assistant.core;

import com.assistant.codegen.modules.gen.domain.CodeGenConfigurationProperties;
import com.assistant.codegen.modules.gen.domain.CodeGenerator;
import com.assistant.codegen.modules.model.domain.Model;
import com.assistant.codegen.modules.model.domain.Project;
import com.assistant.codegen.modules.model.domain.gateway.ColumnGateway;
import com.assistant.codegen.modules.model.domain.gateway.ModelGateway;
import com.assistant.codegen.modules.model.domain.gateway.ProjectGateway;
import com.assistant.codegen.modules.template.domain.Plan;
import com.assistant.codegen.modules.template.domain.Template;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@SpringBootTest
public class CodeGen {

    @Autowired
    private ProjectGateway projectGateway;
    @Autowired
    private ModelGateway modelGateway;
    @Autowired
    private ColumnGateway columnGateway;

    @Test
    public void t() throws IOException, SQLException {
        final CodeGenConfigurationProperties context = new CodeGenConfigurationProperties();
        context.setBasePath("/Users/yingjianhua/IdeaProjects/djwms_service/wms-service");
        final CodeGenerator codeGenerator = new CodeGenerator();
        final Project project = projectGateway.getById(2L);
        final List<Model> models = project.getModels();
        for (Model model : models) {
            model.syncFromDataSource("rowid", "fcreatetime", "fupdatetime", "isdel");
            modelGateway.save(model);
            final Plan plan = model.getPlan();
            final List<Template> templates = plan.getTemplates();
            for (Template template : templates) {
                codeGenerator.gen(context, model, template, true);
            }
        }
    }

//    @Test
    public void t2() throws IOException, SQLException {
        final CodeGenConfigurationProperties context = new CodeGenConfigurationProperties();
        context.setBasePath("/Users/yingjianhua/IdeaProjects/assistant/assistant-codegen");
        final CodeGenerator codeGenerator = new CodeGenerator();
        final Project project = projectGateway.getById(1L);
        final List<Model> models = project.getModels();
        for (Model model : models) {
            model.syncFromDataSource();
            modelGateway.save(model);
            final Plan plan = model.getPlan();
            final List<Template> templates = plan.getTemplates();
            for (Template template : templates) {
                codeGenerator.gen(context, model, template, false);
            }
        }
    }
}
